using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using LevelData;

namespace GGJ11
{
    class GameState : LayerPlayer
    {
        private Level level;


        public GameState(ContentManager content)
            :base(content)
        {
            level = content.ServiceProvider.GetService(typeof(Level)) as Level;
        }


        //update input
        public void Update(GameTime gameTime)
        {
            UpdateInput();
            if(!level.storyMode)
                base.Update(gameTime);
        }
        
        //get input and modify movement value of player
        public void UpdateInput()
        {

#if WINDOWS || XBOX
            //-----------------TEST TAP WITH MOUSE---------------------------------

            KeyboardState keyboardState = Keyboard.GetState();
            if (level.playerLevel.isAlive == false)
            {
                if (Mouse.GetState().LeftButton == ButtonState.Pressed ||
                    GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Start) ||
                    keyboardState.IsKeyDown(Keys.CapsLock))
                    Restart();
            }
            else if (!level.storyMode)
            {
                if (GamePad.GetState(PlayerIndex.One).IsConnected)
                {
                    Accelerometer.Update();
                    level.playerLevel.movement = Accelerometer.GetState().Acceleration.X;
                }
                else
                {

                    // Ignore small movements to prevent running in place.
                    if (Math.Abs(level.playerLevel.movement) < 0.5f)
                        level.playerLevel.movement = 0.0f;

                    //// If any digital horizontal movement input is found, override the analog movement.
                    if (keyboardState.IsKeyDown(Keys.Left))
                    {
                        level.playerLevel.movement = -1.0f;
                    }
                    else if (keyboardState.IsKeyDown(Keys.Right))
                    {
                        level.playerLevel.movement = 1.0f;
                    }
                    else if (keyboardState.IsKeyDown(Keys.A))
                    {
                        level.playerLevel.movement = -0.4f;
                    }
                    else if (keyboardState.IsKeyDown(Keys.D))
                    {
                        level.playerLevel.movement = 0.3f;
                    }

                }

                if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
                    Mouse.GetState().X > GraphicsDeviceManager.DefaultBackBufferWidth / 2 ||
                    GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) ||
                    keyboardState.IsKeyDown(Keys.Up))
                {
                    level.playerLevel.isJumping = true;
                }
                else
                {
                    level.playerLevel.isJumping = false;
                }

                if (Mouse.GetState().LeftButton == ButtonState.Pressed && Mouse.GetState().X < GraphicsDeviceManager.DefaultBackBufferWidth / 2 ||
                    GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.X) || keyboardState.IsKeyDown(Keys.LeftControl)
                    && level.playerLevel.onFighting)
                {
                    level.playerLevel.timeAttack = 0.6f;
                    level.playerLevel.onFighting = false;
                    level.soundList.Add(SoundType.battlePlayer);
                }

                if (level.playerLevel.onExit == true)
                    if (Mouse.GetState().LeftButton == ButtonState.Pressed ||
                        GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Start) ||
                        keyboardState.IsKeyUp(Keys.CapsLock))
                        level.playerLevel.exit = true;

                if (keyboardState.IsKeyDown(Keys.Tab))
                    level.playerLevel.exit = true;
            }
            else if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.RightStick) ||
                    keyboardState.IsKeyDown(Keys.Escape))
            {
                level.storyMode = false;
                level.soundList.Add(SoundType.musicPlay);
                level.soundList.Add(SoundType.storyStop);
            }


#endif
            #if WINDOWS_PHONE
            AccelerometerState accelerometer = Accelerometer.GetState();

            if (Math.Abs(accelerometer.Acceleration.Y) > 0.005f)
            {

                if (accelerometer.Acceleration.Y < 0.0f)

                    level.playerLevel.movement = -accelerometer.Acceleration.Y * 1.5f;

                else if (accelerometer.Acceleration.Y > 0.0f)

                    level.playerLevel.movement = -accelerometer.Acceleration.Y * 1.5f;

            }

            while (TouchPanel.IsGestureAvailable)
            {
                var touch = TouchPanel.ReadGesture();
                if (level.playerLevel.isAlive == false)
                {
                    if (touch.GestureType == GestureType.Tap)
                        Restart();
                }
                else if (!level.storyMode)
                {

                    if (touch.Position.X > GraphicsDeviceManager.DefaultBackBufferWidth / 2)
                    {
                        level.playerLevel.isJumping = true;
                    }
                    else
                    {
                        level.playerLevel.isJumping = false;
                    }

                    if (touch.Position.X < GraphicsDeviceManager.DefaultBackBufferWidth / 2
                       && level.playerLevel.onFighting)
                    {
                        level.playerLevel.timeAttack = 0.6f;
                        level.playerLevel.onFighting = false;
                        level.soundList.Add(SoundType.battlePlayer);
                    }

                    if (level.playerLevel.onExit == true)
                        if (touch.GestureType == GestureType.Tap)
                            level.playerLevel.exit = true;

                }
                else if (touch.GestureType == GestureType.Tap)
                {
                    level.storyMode = false;
                    level.soundList.Add(SoundType.musicPlay);
                    level.soundList.Add(SoundType.storyStop);
                }
            }
            #endif
        }


        //update service whene load the next level
        public void UpdateLevelService(ContentManager content)
        {
            level = content.ServiceProvider.GetService(typeof(Level)) as Level;
            base.UpdateLevelService(content);
        
        }
    }
}
